
using System;
using System.Collections.Generic;
using System.Linq;
namespace Daemon.Model.ViewModel
{
    public delegate void ChangedDelegate();

    public class ImportDataResult
    {
        public string Key { get; }

        public event ChangedDelegate ChangedEvent;

        public ImportDataResult()
        {
            Key = Guid.NewGuid().ToString("N");
        }

        private int _totalTableCount;

        public int TotalTableCount
        {
            get => _totalTableCount;
            set
            {
                if (_totalTableCount != value)
                {
                    ChangedEvent?.Invoke();
                    _totalTableCount = value;
                }
            }
        }

        private int _processedRowCount;

        public int ProcessedRowCount
        {
            get => _processedRowCount;
            set
            {
                if (_processedRowCount != value)
                {
                    ChangedEvent?.Invoke();
                    _processedRowCount = value;
                }
            }
        }

        private ImportTableResult _currentTableResult;

        public ImportTableResult CurrentTableResult
        {
            get => _currentTableResult;
            set
            {
                _currentTableResult = value;
                _currentTableResult.ChangedEvent += Changed;
                ChangedEvent?.Invoke();
            }
        }

        public IDictionary<string, ImportTableResult> TableResults { get; set; }

        public bool Finished { get; set; }

        public bool Failed { get; set; }

        public string Error { get; set; }

        public void Changed()
        {
            ChangedEvent?.Invoke();
        }
    }

    public class ImportTableResult
    {
        public ImportTableResult(string name, int index)
        {
            Name = name;
            Index = index;
        }

        private readonly List<string> _errors = new List<string>();

        private readonly List<string> _messages = new List<string>();

        public event ChangedDelegate ChangedEvent;
        public IEnumerable<string> Errors { get; set; }

        public IEnumerable<string> Messages { get; set; }

        public int Index { get; set; }

        public bool Finished { get; set; }

        public string Name { get; }

        public int SkippedCount { get; set; }

        public int InsertedCount { get; set; }

        public int FailedCount { get; set; }

        public int ProcessedCount { get; set; }

        public int TotalSourceCount { get; set; }

        public void AddError(string error)
        {
            _errors.Add(error);
        }

        public void AddMessage(string message)
        {
            _messages.Add(message);
            ChangedEvent?.Invoke();
        }

        public void Finish()
        {
            Errors = _errors;
            Messages = _messages.OrderBy(r => r, new ImportLogMessageComparer()).ToList();
            Finished = true;
            ChangedEvent?.Invoke();
        }
    }

    public class ImportLogMessageComparer : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            if (!int.TryParse(x.Split(':')[0].Replace("Line", string.Empty).Trim(), out int indexOne))
            {
                return x.CompareTo(y);
            }

            if (!int.TryParse(y.Split(':')[0].Replace("Line", string.Empty).Trim(), out int indexTwo))
            {
                return x.CompareTo(y);
            }

            // To sort in descending order, return the reverse of the comparison
            return indexOne.CompareTo(indexTwo);
        }
    }
}